package com.ayuku.commons.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ayuku.commons.Util;
import com.ayuku.commons.business.user.UserImpl;
import com.ayuku.retail.persistence.model.AppUsers;

/**
 * 
 * @author putu kusuma <putukn@gmail.com>
 * 
 */
public class LockDialog extends org.eclipse.swt.widgets.Dialog {

	public Shell dialogShell;
	private Label label1;
	private Label label2;
	private Button btnExit;
	private Button btnOk;
	private Text txtPassword;

	/**
	 * Auto-generated main method to display this org.eclipse.swt.widgets.Dialog
	 * inside a new Shell.
	 */
	public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			LockDialog inst = new LockDialog(shell, SWT.NULL);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public LockDialog(Shell parent, int style) {
		super(parent, style);
		dialogShell = new Shell(parent, SWT.APPLICATION_MODAL);
		dialogShell.setBackground(SWTResourceManager.getColor(128, 128, 255));

	}

	public void open() {
		try {
			dialogShell.setLayout(new GridLayout(3, false));
			{
				label1 = new Label(dialogShell, SWT.NONE);
				label1.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true,
						false, 1, 1));
				label1.setText("Password: ");

				label1.setAlignment(SWT.RIGHT);
				label1.setBackground(SWTResourceManager.getColor(128, 128, 255));
				label1.setForeground(SWTResourceManager.getColor(255, 255, 255));
			}
			new Label(dialogShell, SWT.NONE);
			new Label(dialogShell, SWT.NONE);
			{
				txtPassword = new Text(dialogShell, SWT.BORDER);
				txtPassword.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
						true, false, 1, 1));
				txtPassword.setTextLimit(30);

				txtPassword.setEchoChar('*');
				txtPassword.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						txtPasswordKeyReleased(evt);
					}
				});
			}
			{
				btnOk = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
				btnOk.setText(" Ok ");

				btnOk.setBackground(SWTResourceManager.getColor(128, 128, 255));
				btnOk.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						btnOkWidgetSelected(evt);
					}
				});
			}
			{
				btnExit = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
				btnExit.setText(" Exit ");

				btnExit.setBackground(SWTResourceManager
						.getColor(128, 128, 255));
				btnExit.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						btnExitWidgetSelected(evt);
					}
				});
			}
			new Label(dialogShell, SWT.NONE);
			new Label(dialogShell, SWT.NONE);
			new Label(dialogShell, SWT.NONE);

			label2 = new Label(dialogShell, SWT.NONE);
			label2.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true,
					false, 3, 1));
			label2.setText("Program terkunci, untuk membebaskannya masukkan password!");

			label2.setBackground(SWTResourceManager.getColor(128, 128, 255));

			dialogShell.layout();
			dialogShell.pack();
			Util.setCenter(dialogShell);
			dialogShell.open();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void btnOkWidgetSelected(SelectionEvent evt) {
		String password = txtPassword.getText();
		if (password == null || password.trim().equals("")) {
			Util.showMsgBox("Password masih kosrong!", 1, dialogShell);
			txtPassword.setFocus();
			return;
		}

		AppUsers currentUser = null;
		try {
			System.out.println("Username: " + Util.getCurrentUser().getUname());
			System.out.println("Password: " + txtPassword.getText());
			currentUser = UserImpl.doLogin(Util.getCurrentUser().getUname(),
					txtPassword.getText());

		} catch (Exception e) {
			currentUser = null;
		}
		if (currentUser == null) {
			Util.showMsgBox("Unlock gagal salah password!", 1, dialogShell);
			txtPassword.setText("");
			txtPassword.setFocus();
			return;
		} else {
			dialogShell.dispose();
		}
	}

	private void btnExitWidgetSelected(SelectionEvent evt) {

		System.exit(0);
		dialogShell.dispose();
	}

	private void txtPasswordKeyReleased(KeyEvent evt) {
		if (evt.keyCode == 13) {
			btnOkWidgetSelected(null);
		}
	}

}
